//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartFunnelView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartFunnelView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartFunnelView

IMPLEMENT_DYNCREATE(CChartFunnelView, CBCGPChartExampleView)

CChartFunnelView::CChartFunnelView()
	: CBCGPChartExampleView(CChartFunnelView::IDD)
{
	//{{AFX_DATA_INIT(CChartFunnelView)
	m_bShowDataLabels = TRUE;
	m_nDataLabelPosition = 0;
	m_nVerticalGap = 5;
	m_bDataLabelsInColimns = FALSE;
	m_nTransparency = 10;
	m_nNeckHeightPerc = 25;
	m_nNeckHeightType = 0;
	m_nNeckHeightValIndex = 0;
	//}}AFX_DATA_INIT
}

CChartFunnelView::~CChartFunnelView()
{
}

void CChartFunnelView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartFunnelView)
	DDX_Control(pDX, IDC_NECK_HEIGHT_VAL, m_wndNeckHeightVal);
	DDX_Control(pDX, IDC_NECK_HEIGHT_PER, m_wndNeckHeightPerc);
	DDX_Control(pDX, IDC_TRANSPARENCY, m_wndTransparency);
	DDX_Control(pDX, IDC_DATA_LABEL_IN_COLUMNS, m_wndDataLabelsInColumns);
	DDX_Control(pDX, IDC_VERTICAL_GAP, m_wndVerticalGap);
	DDX_Control(pDX, IDC_DATA_LABEL_POSITION, m_wndDataLabelPosition);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_POSITION, m_nDataLabelPosition);
	DDX_Slider(pDX, IDC_VERTICAL_GAP, m_nVerticalGap);
	DDX_Check(pDX, IDC_DATA_LABEL_IN_COLUMNS, m_bDataLabelsInColimns);
	DDX_Slider(pDX, IDC_TRANSPARENCY, m_nTransparency);
	DDX_Slider(pDX, IDC_NECK_HEIGHT_PER, m_nNeckHeightPerc);
	DDX_Radio(pDX, IDC_NECK_HEIGHT1, m_nNeckHeightType);
	DDX_CBIndex(pDX, IDC_NECK_HEIGHT_VAL, m_nNeckHeightValIndex);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartFunnelView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartFunnelView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_WM_HSCROLL()
	ON_CBN_SELENDOK(IDC_DATA_LABEL_POSITION, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_LABEL_IN_COLUMNS, OnUpdateChart)
	ON_BN_CLICKED(IDC_NECK_HEIGHT1, OnUpdateChart)
	ON_BN_CLICKED(IDC_NECK_HEIGHT2, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_NECK_HEIGHT_VAL, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartFunnelView diagnostics

#ifdef _DEBUG
void CChartFunnelView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartFunnelView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartFunnelView message handlers

void CChartFunnelView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(m_Feature == CDemoFeature::BCGP_Funnel3D ? BCGPChartFunnel3D : BCGPChartFunnel);

	pChart->SetChartTitle(_T("Site visitors from US:"));

	pChart->AddChartData(_T("California"), 6700);
	pChart->AddChartData(_T("Virginia"), 4500);
	pChart->AddChartData(_T("New York"), 2200);
	pChart->AddChartData(_T("Pennsylvania"), 1500);
	pChart->AddChartData(_T("Georgia"), 1400);
	pChart->AddChartData(_T("Colorado"), 1350);
	pChart->AddChartData(_T("Other States"), 1700);

	m_wndNeckHeightVal.AddString(_T("1400"));
	m_wndNeckHeightVal.AddString(_T("1700"));
	m_wndNeckHeightVal.AddString(_T("2200"));
	m_wndNeckHeightVal.AddString(_T("4500"));

	m_wndNeckHeightVal.SetCurSel(0);

	m_wndVerticalGap.SetRange(0, 20, TRUE);
	m_wndVerticalGap.SetPos(m_nVerticalGap);

	m_wndNeckHeightPerc.SetRange(0, 100, TRUE);
	m_wndNeckHeightPerc.SetPos(m_nNeckHeightPerc);

	m_wndTransparency.SetRange(0, 100, TRUE);

	pChart->SetSeriesShadow(m_Feature != CDemoFeature::BCGP_Funnel3D);

	m_bIsReady = TRUE;

	OnUpdateChart();
}

void CChartFunnelView::OnUpdateChart() 
{
	UpdateData();

	m_wndDataLabelPosition.EnableWindow(m_bShowDataLabels);
	m_wndDataLabelsInColumns.EnableWindow(m_bShowDataLabels);

	m_wndNeckHeightPerc.EnableWindow(m_nNeckHeightType == 0);
	m_wndNeckHeightVal.EnableWindow(m_nNeckHeightType == 1);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataLabels(m_bShowDataLabels);

	if (m_bShowDataLabels)
	{
		BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();

		switch (m_nDataLabelPosition)
		{
		case 0:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_DEFAULT_POS;
			break;

		case 1:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
			dataLabelOptions.m_dblAngle = -90;
			break;

		case 2:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
			dataLabelOptions.m_dblAngle = 90;
			break;

		case 3:
			dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_CENTER;
			m_wndDataLabelsInColumns.EnableWindow(FALSE);
			break;
		}

		pChart->SetDataLabelsOptions(dataLabelOptions);
	}

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartFunnelSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartFunnelSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		if (m_nNeckHeightType == 0)
		{
			pSeries->SetNeckHeightInPercents(m_nNeckHeightPerc);
		}
		else
		{
			CString strVal;
			m_wndNeckHeightVal.GetLBText(m_nNeckHeightValIndex, strVal);
			
			pSeries->SetNeckHeightInChartValues(_ttol(strVal));
		}

		pSeries->m_bDataLabelsInColumns = m_bDataLabelsInColimns;
		pSeries->SetGap(m_nVerticalGap);
	}
	
	pChart->SetThemeOpacity(100 - m_nTransparency);
	pChart->Redraw();
}

void CChartFunnelView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();
	}
}
